//
//  CQMineMainView.m
//  HiChat
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 wqiong. All rights reserved.
//

#import "CQMineMainView.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeigh [UIScreen mainScreen].bounds.size.height
#define rectLeftArm CGRectMake(1, 90, 40, 65)
#define rectRightArm CGRectMake(header.frame.size.width / 2 + 60, 90, 40, 65)
#define rectLeftHand CGRectMake(kWidth/ 2 - 100, loginview.frame.origin.y - 22, 40, 40)
#define rectRightHand CGRectMake(kWidth/ 2 + 62, loginview.frame.origin.y - 22, 40, 40)




@interface CQMineMainView()<UITextFieldDelegate, IChatManagerDelegate>

//猫的手
@property (strong,nonatomic) UIImageView *lefthand;
@property (strong,nonatomic) UIImageView *righthand;


//猫的蒙眼胳膊
@property (strong,nonatomic) UIImageView *lefthArm;
@property (strong,nonatomic) UIImageView *rightArm;
@property (assign,nonatomic) ClickType clicktype;

@end


@implementation CQMineMainView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self create];
    }
    return self;
}
- (void)create {
    UIImageView* header = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth / 2 - 211 / 2, 100, 211, 109)];
    
    header.image=[UIImage imageNamed:@"header"];
    [self addSubview:header];
    
    _lefthArm = [[UIImageView alloc]initWithFrame:rectLeftArm];
    _lefthArm.image=[UIImage imageNamed:@"left"];
    [header addSubview:_lefthArm];
    
    
    _rightArm = [[UIImageView alloc]initWithFrame:rectRightArm];
    _rightArm.image=[UIImage imageNamed:@"right"];
    [header addSubview:_rightArm];
    
    
    UIView *loginview=[[UIView alloc]initWithFrame:CGRectMake(15, 200, kWidth-30, 160)];
    loginview.layer.borderWidth=1;
    loginview.layer.borderColor=[UIColor lightGrayColor].CGColor;
    loginview.backgroundColor=[UIColor colorWithWhite:0.9 alpha:1];
    [self addSubview:loginview];
    
    _lefthand = [[UIImageView alloc]initWithFrame:rectLeftHand];
    _lefthand.image = [UIImage imageNamed:@"hand"];
    [self addSubview:_lefthand];
    
    _righthand = [[UIImageView alloc]initWithFrame:rectRightHand];
    _righthand.image = [UIImage imageNamed:@"hand"];
    [self addSubview:_righthand];
    
    _userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 30, kWidth - 90, 44)];
    _userNameTextField.delegate = self;
    _userNameTextField.layer.cornerRadius = 5;
    _userNameTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _userNameTextField.layer.borderWidth = 0.7;
    _userNameTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    _userNameTextField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *userimg = [[UIImageView alloc]initWithFrame:CGRectMake(11, 11, 22, 22)];
    userimg.image = [UIImage imageNamed:@"user"];
    [_userNameTextField.leftView addSubview:userimg];
    [loginview addSubview:_userNameTextField];
    
    
    
    _passWordTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 90, kWidth - 90, 44)];
    _passWordTextField.delegate = self;
    _passWordTextField.layer.cornerRadius = 5;
    _passWordTextField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _passWordTextField.layer.borderWidth = 0.5;
    _passWordTextField.secureTextEntry = YES;
    _passWordTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    _passWordTextField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView* pssimag = [[UIImageView alloc] initWithFrame:CGRectMake(11, 11, 22, 22)];
    pssimag.image = [UIImage imageNamed:@"pass"];
    [_passWordTextField.leftView addSubview:pssimag];
    [loginview addSubview:_passWordTextField];
    
    
    UIButton *registButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registButton.frame = CGRectMake(loginview.frame.origin.x + 40, loginview.frame.size.height + loginview.frame.origin.y + 40, (loginview.frame.size.width - 110) / 2, 40);
    registButton.layer.cornerRadius = 10;
    [registButton setTitle:@"注   册" forState:UIControlStateNormal];
    [registButton addTarget:self action:@selector(registButAction) forControlEvents:UIControlEventTouchUpInside];
    registButton.backgroundColor = [UIColor redColor];
    [self addSubview:registButton];
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(registButton.frame.origin.x + registButton.frame.size.width + 30, registButton.frame.origin.y, registButton.frame.size.width, 40);
    loginButton.layer.cornerRadius = 10;
    [loginButton setTitle:@"登   录" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButAction) forControlEvents:UIControlEventTouchUpInside];
    loginButton.backgroundColor = [UIColor blackColor];
    [self addSubview:loginButton];

}
- (void)registButAction {
    [self.userNameTextField resignFirstResponder];
    [self.passWordTextField resignFirstResponder];
    
    [self.delegate ChangeMainVCToLoginVC];
}
- (void)loginButAction {
    [self.userNameTextField  resignFirstResponder];
    [self.passWordTextField resignFirstResponder];
    
    if (self.userNameTextField.text.length == 0 || self.passWordTextField.text.length == 0) {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"用户名或密码不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    } else {
        //初始化环信SDK,apnsCertName为推送证书
        [[EaseMob sharedInstance] registerSDKWithAppKey:APPKEY apnsCertName:nil];
        //异步登陆的方法
        [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userNameTextField.text password:self.passWordTextField.text completion:^(NSDictionary *loginInfo, EMError *error) {
            //获取数据库中数据
            [[EaseMob sharedInstance].chatManager loadDataFromDatabase];
            
            [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
            
            // 获取好友列表
            [[EaseMob sharedInstance].chatManager asyncFetchBuddyList];

        
            NSLog(@"登陆成功");
            NSUserDefaults *userName = [NSUserDefaults standardUserDefaults];
            [userName setObject:self.userNameTextField.text forKey:@"userName"];
            [userName setObject:self.passWordTextField.text forKey:@"userPassword"];
            [self.delegate ChangeMainVCToLoginedVC];
            
            
        } onQueue:nil];
    }
}
/*!
 @method
 @brief 获取好友列表成功时的回调
 @discussion
 @param buddyList 好友列表
 @param error     错误信息
 */
- (void)didFetchedBuddyList:(NSArray *)buddyList
                      error:(EMError *)error
{
    NSLog(@"获取好友列表成功");
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    if ([textField isEqual:_userNameTextField]) {
        if (_clicktype != clicktypePass) {
            _clicktype =clicktypeUser;
            return;
        }
        _clicktype=clicktypeUser;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.lefthArm.frame = CGRectMake(self.lefthArm.frame.origin.x - 60, self.lefthArm.frame.origin.y + 30, self.lefthArm.frame.size.width, self.lefthArm.frame.size.height);
            
            self.rightArm.frame = CGRectMake(self.rightArm.frame.origin.x+48, self.rightArm.frame.origin.y + 30, self.rightArm.frame.size.width, self.rightArm.frame.size.height);
            
            self.lefthand.frame = CGRectMake(self.lefthand.frame.origin.x-70, self.lefthand.frame.origin.y, 40, 40);
            self.righthand.frame = CGRectMake(self.righthand.frame.origin.x +30, self.righthand.frame.origin.y, 40, 40);
        } completion:^(BOOL finished) {
            
        }];
    }else if ([textField isEqual:_passWordTextField]){
        if (_clicktype == clicktypePass)
        {
            return;
        }
        _clicktype = clicktypePass;
        
        [UIView animateWithDuration:0.5 animations:^{
            self.lefthArm.frame = CGRectMake(self.lefthArm.frame.origin.x + 60, self.lefthArm.frame.origin.y - 30, self.lefthArm.frame.size.width, self.lefthArm.frame.size.height);
            
            self.rightArm.frame = CGRectMake(self.rightArm.frame.origin.x - 48, self.rightArm.frame.origin.y - 30, self.rightArm.frame.size.width, self.rightArm.frame.size.height);
            
            self.lefthand.frame = CGRectMake(self.lefthand.frame.origin.x + 70, self.lefthand.frame.origin.y, 0, 0);
            self.righthand.frame = CGRectMake(self.righthand.frame.origin.x - 30, self.righthand.frame.origin.y, 0, 0);
        } completion:^(BOOL finished) {
            
        }];
    }
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.userNameTextField  resignFirstResponder];
    [self.passWordTextField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
